#pragma once

#include <afxsock.h>
#include "MyThread.h"

class CNetChatServer : public CSocket
{
public:
	CNetChatServer(void);
};

class CNetChatClient
{
public:
	class IListener
	{
	public:
		virtual void OnRecive(const BYTE* pBuffer, size_t nLen) = 0;
		virtual void OnDisconnected(void) = 0;
		virtual void OnEnd(void) = 0;
	};

	CNetChatClient(void);
	virtual ~CNetChatClient(void);

	bool Start(LPCTSTR lpszHostAddress, UINT nHostPort, IListener& rListener, size_t nMaxBufferLen);
	void Close();

private:
	class CRecivingWorker : public CMyThread::IWorker
	{
	public:
		CRecivingWorker(IListener& rListener, size_t nMaxBufferLen);
		~CRecivingWorker(void);

		virtual bool Work();
		virtual bool IsEnding();
		virtual void Terminated();

		bool Connect(LPCTSTR lpszHostAddress, UINT nHostPort);
		void End();
	private:
		bool m_IsEnd;
		bool m_IsTerminated;
		CSocket m_socketReciver;
		IListener& m_rListener;
		size_t m_nMaxBufferLen;
		BYTE* m_pBuffer;
	};

private:
	CRecivingWorker* m_RecivingWorker;
	CMyThread m_Thread;
};
